package com.xavier.dong.nacos.grpc.client.controller;

import com.xavier.dong.nacos.grpc.client.grpc.GrpcClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Optional demo controller making the grpc service accessible via browser requests.
 */
@RestController
public class GrpcClientController {

    @Autowired
    private GrpcClientService grpcClientService;

    @Value("${auth.username}")
    private String username;

    @RequestMapping("/simple")
    public String printMessage(@RequestParam(defaultValue = "Michael") final String name) {
        return this.grpcClientService.sendSimpleRpc(name);
    }

    @RequestMapping("/s2c")
    public List<String> sendServer2ClientRpc(@RequestBody List<String> name) {
        return this.grpcClientService.sendServer2ClientRpc(name);
    }

    @RequestMapping("/c2s")
    public void sendClient2ServerRpc() throws InterruptedException {
        this.grpcClientService.sendClient2ServerRpc(100);
    }

    @RequestMapping("/bsr")
    public void sendBindirectionalStreamRpc() throws InterruptedException {
        this.grpcClientService.sendBindirectionalStreamRpc(200);
    }

}